package com.gengzp.mq.server.controller;

import com.gengzp.mq.server.enums.MqMessageStatusEnums;
import com.gengzp.mq.server.model.generic.MqPaginationResult;
import com.gengzp.mq.server.model.generic.MqResult;
import com.gengzp.mq.server.model.message.req.*;
import com.gengzp.mq.server.model.message.resp.MqPageMessageResp;
import com.gengzp.mq.server.model.message.resp.MqReceiveMessageResp;
import com.gengzp.mq.server.service.MqMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName MqMessageController
 * @Description mq消息管理模块控制层
 * @Author gengzp
 * @Date 2025/9/4 15:27
 */
@Controller
@RequestMapping("/mq/message")
public class MqMessageController {

    private static final Logger logger = LoggerFactory.getLogger(MqMessageController.class);

    @Autowired
    private MqMessageService mqMessageService;

    /**
     * 生产者发送消息
     */
    @ResponseBody
    @PostMapping("/send")
    public MqResult<String> sendMessage(@RequestBody MqSendMessageReq req) {
        // topic 空值校验
        if (req.getTopic() == null || req.getTopic().isBlank()) {
            return MqResult.fail("消息主题 topic 不能为空");
        }
        // content 空值校验
        if (req.getContent() == null || req.getContent().isBlank()) {
            return MqResult.fail("消息内容 content 不能为空");
        }
        try {
            // 发送消息
            return MqResult.success(mqMessageService.sendMessage(req));
        } catch (Exception e) {
            logger.error("发送消息失败: " + e.getMessage(), e);
            return MqResult.fail("发送消息失败, 原因: " + e.getMessage());
        }
    }

    /**
     * 消费者获取消息
     * 如果无消息, 该方法会阻塞等待
     */
    @ResponseBody
    @PostMapping("/receive")
    public MqResult<MqReceiveMessageResp> receiveMessage(@RequestBody MqReceiveMessageReq req) {
        // topic 空值校验
        if (req.getTopic() == null || req.getTopic().isBlank()) {
            return MqResult.fail("消息主题 topic 不能为空");
        }
        try {
            // 接收消息
            return MqResult.success(mqMessageService.receiveMessage(req));
        } catch (Exception e) {
            logger.error("接收消息失败: " + e.getMessage(), e);
            return MqResult.fail("接收消息失败, 原因: " + e.getMessage());
        }
    }

    /**
     * 生产者确认消息
     */
    @ResponseBody
    @PostMapping("/ack")
    public MqResult<Void> ackMessage(@RequestBody MqAckMessageReq req) {
        // messageId 空值校验
        if (req.getMessageId() == null || req.getMessageId().isBlank()) {
            return MqResult.fail("消息ID messageId 不能为空");
        }
        try {
            mqMessageService.ackMessage(req);
            return MqResult.success();
        } catch (Exception e) {
            logger.error("确认消息失败: " + e.getMessage(), e);
            return MqResult.fail("确认消息失败, 原因: " + e.getMessage());
        }
    }

    /**
     * 直接重发消息
     */
    @ResponseBody
    @PostMapping("/resend")
    public MqResult<String> resendMessage(@RequestBody MqResendMessageReq req) {
        if (req.getMessageId() == null || req.getMessageId().isBlank()) {
            return MqResult.fail("消息ID messageId 不能为空");
        }

        try {
            mqMessageService.resendMessage(req);
            return MqResult.success(req.getMessageId());
        } catch (Exception e) {
            logger.error("消息重发失败: " + e.getMessage(), e);
            return MqResult.fail("消息重发失败, 原因: " + e.getMessage());
        }
    }

    /**
     * 分页查询消息
     */
    @ResponseBody
    @PostMapping("/page")
    public MqResult<MqPaginationResult<MqPageMessageResp>> pageQueryMessage(@RequestBody MqPageMessageReq req) {
        // pageNum 校验
        if (req.getPageNum() == null || req.getPageNum() < 1) {
            return MqResult.fail("页码 pageNum 不能小于1");
        }
        // pageSize 校验
        if (req.getPageSize() == null || req.getPageSize() < 1) {
            return MqResult.fail("每页数量 pageSize 不能小于1");
        }
        // status 校验
        if (req.getStatus() != null && MqMessageStatusEnums.getByCode(req.getStatus()) == null) {
            return MqResult.fail("消息状态 status 枚举值不合法");
        }
        try {
            return MqResult.success(mqMessageService.pageQueryMessage(req));
        } catch (Exception e) {
            logger.error("分页查询消息失败: " + e.getMessage(), e);
            return MqResult.fail("分页查询消息失败, 原因: " + e.getMessage());
        }
    }

}
